package pre;

import it.unisa.dia.gas.jpbc.Element;
import it.unisa.dia.gas.jpbc.Field;
import it.unisa.dia.gas.jpbc.Pairing;
import it.unisa.dia.gas.plaf.jpbc.field.curve.CurveElement;
import it.unisa.dia.gas.plaf.jpbc.pairing.PairingFactory;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TPRE {
    Pairing pairing ;
    Field G1;
    Field GT;
    Field Zq;
    Element g,g1,h1,h2,h3,h4;
    Element one_g ,one_z;

    public void setup(){

        BigInteger p = new BigInteger("8780710799663312522437781984754049815806883199414208211028653399266475630880222957078625179422662221423155858769582317459277713367317481324925129998224791");

//        this.pairing = PairingFactory.getPairing(new TypeACurveGenerator(security_parameter * 5 / 32, security_parameter / 2).generate());
        this.pairing = PairingFactory.getPairing("resource/a.properties");
        this.G1 = pairing.getG1();
        this.GT = pairing.getGT();
        this.Zq = pairing.getZr();
        this.g =  G1.newRandomElement();

        this.one_g =  this.G1.newOneElement();
        this.one_z = this.Zq.newOneElement();
    }

    public Map<String,Element> keyGen(){
        Element x = Zq.newRandomElement();
        Element pk = g.powZn(x);
        Element sk = x;
        Map<String,Element> keypair = new HashMap<>();
        keypair.put("pk",pk);
        keypair.put("sk",sk);
        return keypair;
    }

    public List<Element> reKeyGen(Element pk_b, Element sk_a){
        return null;
    }

    public static void main(String[] args) {
        TPRE tpre = new TPRE();
        tpre.setup();
        Map<String,Element> alice = tpre.keyGen();
        Map<String,Element> bob = tpre.keyGen();
        Element alice_pk = alice.get("pk");
        Element alice_sk = alice.get("sk");
        Element bob_pk = alice.get("pk");
        Element bob_sk = alice.get("sk");
        Element chars_pk = alice.get("pk");
        Element chars_sk = alice.get("sk");
        Element r = tpre.G1.newRandomElement().getImmutable();
        Element sk1 = tpre.Zq.newRandomElement().getImmutable();
        Element sk2 = tpre.Zq.newRandomElement().getImmutable();
        Element sk3 = tpre.Zq.newRandomElement().getImmutable();
        Element g = tpre.g;
//        Element one_z =


        Element _1_sk = tpre.one_z.duplicate().div(sk1).add(tpre.one_z.duplicate().div(sk2)).add(tpre.one_z.duplicate().div(sk3));
        System.out.println("_1_sk:"+_1_sk);
        Element sk_g =  sk1.mul(g).mul(sk2).mul(sk3);
//        Element _1_sk_g  = tpre.one.duplicate().div(sk_g);

        Element sk1_o = sk_g.div(sk1.mul(g));
        Element sk2_o = sk_g.div(sk2.mul(g));
        Element sk3_o = sk_g.div(sk3.mul(g));

        Element _1_sk_2 = (sk1_o.add(sk2_o).add(sk3_o)).div(sk_g);

        System.out.println("_1_sk_g"+_1_sk_2);




//        Element one = tpre.one;
//        Element bob_pk_sk = bob_sk;
//        Element r = tpre.G1.newRandomElement();

    }
}
